import time
import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase


class TestRegister(TestCase):

    def testRegister1(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'delete  from user where username="wangwu" '
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//div[2]/a")
        ele1.click()

        ele2 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele2.send_keys("wangwu")

        ele3 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele3.send_keys("123456")

        ele4 = driver.find_element(By.XPATH, "//input[@id='repassword']")
        ele4.send_keys("123456")

        ele5 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele5.click()

        time.sleep(3)

        url = driver.current_url
        if url != "http://localhost:90":
            driver.save_screenshot("wangwu1-123456.jpg")

        self.assertNotEqual(url, "http://localhost:90")

        driver.quit()

    def testRegister2(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()

        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        self.assertIn(('wangwu1',), x)

        ele1 = driver.find_element(By.XPATH, "//div[2]/a")
        ele1.click()

        ele2 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele2.send_keys("wangwu1")

        ele3 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele3.send_keys("123456")

        ele4 = driver.find_element(By.XPATH, "//input[@id='repassword']")
        ele4.send_keys("123456")

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[1]/span[1]").text

        if result != "用户wangwu1已存在":
            driver.save_screenshot("wangwu1已存在.jpg")

        self.assertEqual(result, "用户wangwu1已存在")

        driver.quit()

    def testRegister3(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'delete  from user where username="wangwu" '
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//div[2]/a")
        ele1.click()

        ele2 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele2.send_keys("wangwu")

        ele3 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele3.send_keys("123456")

        ele4 = driver.find_element(By.XPATH, "//input[@id='repassword']")
        ele4.send_keys("1234567")

        ele5 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele5.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[3]/span[1]").text

        if result != "两次密码不相同":
            driver.save_screenshot("wangwu-123456.jpg")

        self.assertEqual(result, "两次密码不相同")
        driver.quit()
